Identifying errors in algorithms
Objectives : Student should be able to -
Q1. A piece of pseudocode was written to input ten numbers and then calculate and output the largest and average value of the input data.
10Largest = 0
20Sum = 0
30FOR X = 0 TO 10
40INPUT X
50IF X > Largest THEN X = Largest
60OUTPUT Largest
70Sum = Sum + X
80NEXT X
90Average = Sum * 10
100OUTPUT Average
There are four errors in this algorithm. Locate these errors and suggest a correction.
Error 1 : Line 40 : INPUT X is using the same counter variable of FOR-NEXT loop
Correction : Change the counter variable of FOR-NEXT loop.
For example - FOR Count = 1 TO 10
Error 2 : Line 50 : X = Largest
Correction : It should be, Largest = X
Error 3 : Line 60 : Output should not be inside the loop.
Correction : It should be outside the loop.
For example - 85 OUTPUT Largest
Error 4 : Line 90 : Incorrect formula for calculation of average.
Correction : It should be - Average = Sum/10
Q2. Read this section of program code that should input 50 numbers and then output the average.
1Total = 0
2FOR Counter = 1 TO 50
3INPUT Num
4Total = Total + 1
5Counter = Counter + 1
6Average = Total/Counter
7NEXT Counter
8PRINT Average
There are four errors in this code.
Locate these errors and suggest code corrections to remove each error.
Error 1 : Line 4 : Total = Total + 1
Correction : It should be, Total = Total + Num
Error 2 : Line 5 : Counter = Counter + 1
Correction : It should be removed because the counter variable's value increments by 1
automatically in FOR-NEXT loop.
Error 3 : Line 6 : Average = Total/Counter
Correction : It should be outside the loop. Swap line 6 and 7.
Error 4 : Line 6 : Average = Total/Counter
Correction : Incorrect formula for calculation of average.
It should be - Average = Total/50
Q3. An algorithm has been written in pseudocode to input some numbers. It only outputs any numbers that are greater than or equal to 100. The number 999 is not output and stops the algorithm.
INPUT Number
WHILE Numbers < > 999 DO
IF Number > 100
THEN
OUTPUT Number
ENDIF
ENDWHILE
OUTPUT Number
a) Identify the four errors in the pseudocode and suggest corrections.
Error 1 : WHILE Numbers < > 999 DO
Correction : Numbers should be Number. It should be -
WHILE Number < > 999 DO
Correction : It should be, IF Number >= 100
Error 3 : NPUT Number is missing from inside the loop.
Correction : Insert INPUT Number after the ENDIF statement.
Error 4 : The final OUTPUT Number outside the loop is not needed.
Correction : Remove it.
b) Write a pseudocode statement to change the corrected algorithm to output all numbers between 100 and 200 inclusive.
You do not need to rewrite the whole algorithm.
IF Number >= 100 AND Number < = 200
Q4. An algorithm allows a user to input their password and checks that there are at least eight characters in the password. Then, the user is asked to re-input the password to check that both inputs are the same. The user is allowed three attempts at inputting a password of the correct length and a matching pair of passwords.
The pre-defined function LEN(X) returns the number of characters in the string, X.
01Attempt ‹— 0
02REPEAT
03PassCheck ‹— TRUE
04OUTPUT "Please enter your password "
05INPUT Password
06IF LEN(Password) < 8
07THEN
08PassCheck ‹— TRUE
09ELSE
10OUTPUT "Please re-enter your password "
11INPUT Password2
12IF Password < > Password
13THEN
14PassCheck ‹— FALSE
15ENDIF
16ENDIF
17Attempt ‹— Attempt + 1
18UNTIL PassCheck OR Attempt < > 3
19IF PassCheck
20THEN
21OUTPUT "Password success"
22ELSE
23OUTPUT "Password fail"
24ENDIF
a) Identify the three errors in the pseudocode and suggest a correction to remove each error.
Error 1 : Line 8 : PassCheck ‹— TRUE
Correction : It should be, PassCheck ‹— FALSE
Error 2 : Line 12 : IF Password < > Password
Correction : It should be, IF Password < > Password2
Error 3 : Line 18 : UNTIL PassCheck OR Attempt < > 3
Correction : It should be, UNTIL PassCheck OR Attempt = 3
b) The algorithm includes two types of check on the data input.
Identify and describe each type of check
Type of check 1 : Length check validation.
Description : Checks number of characters in password.
Type of check 1 : Double entry verification check.
Description : Ask to input the password twice and compares both to check if they are same.
c) Give two sets of test data for this algorithm and a reason for choosing each set.
Each set of test data and its reason must be different.
Reason : Abnormal data, it should be rejected because it contains characters less than 8.
Reason : Normal data, it should be accepted because it contains characters not less than 8 and both are same.
Q5. An algorithm has been written in pseudocode to generate 50 positive random integers with values less than or equal to 100. These numbers are stored in the array NumRand[ ]
The function RANDOM(X, Y) generates a random integer greater than X and less than or equal to Y.
For example, RANDOM(1, 4) generates 2 or 3 or 4
01Count ‹— 0
02WHILE Counter > 50 DO
03NumRand[Counter] ‹— RANDOM(1, 100)
04Counter ‹— Counter - 2
05ENDWHILE
Find the four errors in the pseudocode and write a correction for each error.
Error 1 : Line 01 : Count ‹— 0
Correction : It should be, Counter ‹— 0
Error 2 : Line 02 : WHILE Counter > 50 DO
Correction : It should be, WHILE Counter < 50 DO
Error 3 : Line 03 : NumRand[Counter] ‹— RANDOM(1, 100)
Correction : It should be, NumRand[Counter] ‹— RANDOM(0, 100)
Error 4 : Line 04 : Counter ‹— Counter - 2
Correction : It should be, Counter ‹— Counter + 1
Q6. The pseudocode algorithm should work as a calculator and output the result.
01Continue ‹— 1
02WHILE Continue = 0
03OUTPUT "Enter 1 for +, 2 for -, 3 for *, or 4 for /"
04INPUT Operator
05OUTPUT "Enter the first value"
06INPUT Value1
07OUTPUT "Enter the second value"
08OUTPUT Value2
09IF Operator
101 : Answer ‹— Value1 + Value2
112 : Answer ‹— Value1 - Value2
123 : Answer ‹— Value1 * Value2
134 : Answer ‹— Value1 / Value2
14ENDCASE
15OUTPUT "The answer is ", Value1
16OUTPUT "Do you wish to enter more values (Yes or No)?"
17INPUT MoreValues
18IF MoreValues = "No"
19THEN
20Continue ‹— 1
21ENDIF
22UNTIL Continue = 0
Find the five errors in the pseudocode and suggest a correction for each error.
Error 1 : Line 01 : Continue ‹— 1
Correction : It should be, Continue ‹— 0
Error 2 : Line 22 : UNTIL Continue = 0
Correction : It should be, ENDWHILE
Error 3 : Line 08 : OUTPUT Value2
Correction : It should be, INPUT Value2
Error 4 : Line 09 : IF Operator
Correction : It should be, CASE OF Operator
Error 5 : Line 15 : OUTPUT "The answer is ", Value1
Correction : It should be, OUTPUT "The answer is ", Answer
Error 1 : Line 02 : WHILE Continue = 0
Correction : It should be, REPEAT
Error 2 : Line 20 : Continue ‹— 1
Correction : It should be, Continue ‹— 0
Error 3 : Line 22 : UNTIL Continue = 0
Correction : It should be, UNTIL Continue = 1
Error 4 : Line 08 : OUTPUT Value2
Correction : It should be, INPUT Value2
Error 5 : Line 09 : IF Operator
Correction : It should be, CASE OF Operator
Error 6 : Line 15 : OUTPUT "The answer is ", Value1
Correction : It should be, OUTPUT "The answer is ", Answer
* * * * * * * * *
* * * * * *
* * *
*